package com.hup.utils.swing.frame;

import com.hup.utils.commons.interfaces.Runners.RunAndThrowable;
import com.hup.utils.commons.start.AppIcon;
import com.hup.utils.commons.start.DialogHelper;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.commons.start.VersionConfig;
import com.hup.utils.commons.swing.SwingExecutable;
import com.hup.utils.commons.thread.ThreadPoolUtil;
import com.hup.utils.swing.FrameUtil.VisibleListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * 基础窗口类;
 * 功能
 * <pre>
 *     .设置窗口图标
 *          .(可选)使用前引入 {@link SpringContext}和 {@link AppIcon}
 *     .baseTitle
 *     .execute方法 {@link SwingExecutable}
 *          .默认使用共用的异步执行器 {@link SwingWorker}, {@link ThreadPoolUtil#uiExecutor(String, String)}
 *          .需要执行初始化: {@link SwingExecutable#initSwingExecutor()}
 * </pre>
 *
 * @author hugan
 * @date 2020/5/23
 * @apiNote 继承本的都应该是 {@link Window},swing的[{@link JFrame},{@link JWindow},{@link JDialog}]都是继承自 {@link Window}
 */
public interface BaseWindow extends VisibleListener, SwingExecutable {

    /**
     * {@link Frame#setTitle(String)}, {@link Dialog#setTitle(String)}
     */
    void setTitle(String title);

    /**
     * {@link Window#setVisible(boolean)}
     */
    void setVisible(boolean b);

    /**
     * {@link Window#setIconImage(Image)}
     */
    void setIconImage(Image image);

    /**
     * @return 返回[appName_version(_环境)]标题;其中,[环境]需要在application.properties中配置
     */
    static String getVersionedTitle() {
        VersionConfig versionConfig = SpringContext.getBean(VersionConfig.class);
        final String spliter = "_";
        if (versionConfig.env != null) {
            return versionConfig.appName + spliter + versionConfig.version + spliter + versionConfig.env;
        } else {
            return versionConfig.appName + spliter + versionConfig.version;
        }
    }

    String getBaseTitle();

    void setBaseTitle(String baseTitle);

    /**
     * 设置子标题
     */
    default void setSubTitle(String text) {
        if (text != null) {
            setTitle(getBaseTitle() + " - " + text);
        } else {
            setTitle(getBaseTitle());
        }
    }

    /**
     * @param singleton 是否为单例窗口
     */
    void setSingleton(boolean singleton);

    boolean isSingleton();

    void onCreate();

    default void initIcon() {
        try {
            Image image = SpringContext.getBean(AppIcon.class).getImage();
            setIconImage(image);
        } catch (Exception e) {
            //没有配置框架代码或无图标时,使用java自带图标
            setIconImage(null);
        }
    }

    default ActionListener confirm(String desc, RunAndThrowable runnable) {
        return e -> {
            boolean ok = DialogHelper.yesNo("确认[" + desc + "]?").show((Window) this);
            if (ok) doInBackground(desc, runnable);
        };
    }

    /**
     * {@link WindowListener#windowClosing(WindowEvent)}
     *
     * @return 是否dispose窗口
     */
    boolean onWindowClosing(WindowEvent e);

}
